package thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

public class demo27 {
    static int i=0;
    public static void main(String[] args) {
        BlockingQueue<Integer> bq=new LinkedBlockingDeque<>();
        Thread producer=new Thread(()->{
           //生产者线程
            while(true){
                try {
                    bq.put(i);
                    System.out.println("生产一个元素"+i);
                    i++;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });


        Thread consumer=new Thread(()->{
            while (true){
                try {
                    int p=bq.take();
                    System.out.println("消费一个元素"+p);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        producer.start();
        consumer.start();
    }
}
